package GUI.main.navigation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.AbstractListModel;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import GUI.helpers.ButtonType;
import GUI.helpers.PButton;
import GUI.helpers.ProjectConstants;
import GUI.main.MainClient;
import GUI.main.dialogs.AddPatientDialog;
import GUI.main.dialogs.UserProfile;
import GUI.main.dialogs.WaitingAppointmentsDialog;
import GUI.main.renderers.PatientListRenderer;

import officeserver.log_error.UserException;
import officeserver.users.PersonalInfo;

/**
 * @author mramsey3
 * 
 */
public class DoctorNavigationMenuPanel extends JPanel implements MouseListener,
	ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JPanel titlePanel;
    private JPanel patientPanel;
    private JPanel patientTitlePanel;
    private JPanel patientButtonPanel;
    private PButton createPatient;
    private PButton deletePatient;
    private JScrollPane patientScrollPane;
    private JList patientList;

    private JPanel lowerButtonPanel;
    private JPanel viewWaitingPanel;
    private PButton viewWaitingAppointments;
    private JPanel logoutPanel;
    private PButton logoutButton;

    private NavigationMenuPanel navigationMenuPanel;

    /**
     * 
     */
    public DoctorNavigationMenuPanel(NavigationMenuPanel navigationMenuPanel) {
	super();
	this.navigationMenuPanel = navigationMenuPanel;
	setLayout(new BorderLayout());
	add(getLowerButtonPanel(), BorderLayout.SOUTH);
	add(getPatientPanel(), BorderLayout.CENTER);
	add(getTitlePanel(),BorderLayout.NORTH);
    }
    
    
    public JPanel getTitlePanel() {
	if (titlePanel == null) {
	    titlePanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.HEADER_COLOR);
		    g.fillRect(0, 0, getWidth(), getHeight());
		    g.setColor(Color.white);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
		    g.drawString("DOCTOR", 12, getHeight() - 8);
		}
	    };
	    titlePanel.setPreferredSize(new Dimension(WIDTH, 25));
	}
	return titlePanel;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JPanel getLowerButtonPanel() {
	if (lowerButtonPanel == null) {
	    lowerButtonPanel = new JPanel();
	    lowerButtonPanel.setBorder(new EmptyBorder(20,10,10,10));
	    lowerButtonPanel.setLayout(new BoxLayout(lowerButtonPanel,
		    BoxLayout.PAGE_AXIS));

	    lowerButtonPanel.add(getViewWaitingPanel());
	    lowerButtonPanel.add(getLogoutPanel());
	}
	return lowerButtonPanel;
    }

    /**
     * @return
     */
    private JPanel getViewWaitingPanel() {
	if (viewWaitingPanel == null) {
	    viewWaitingPanel = new JPanel();
	    viewWaitingPanel.setPreferredSize(new Dimension(200, 30));
	    viewWaitingPanel.add(getViewWaitingAppointments());
	}
	return viewWaitingPanel;
    }

    /**
     * @author mramsey3
     * @return
     */
    private PButton getViewWaitingAppointments() {
	if (viewWaitingAppointments == null) {
	    viewWaitingAppointments = new PButton("View Waiting Appointments",ButtonType.LONG);
	    viewWaitingAppointments.addActionListener(this);
	}
	return viewWaitingAppointments;
    }

    /**
     * @return
     */
    private JPanel getLogoutPanel() {
	if (logoutPanel == null) {
	    logoutPanel = new JPanel();
	    logoutPanel.setPreferredSize(new Dimension(200, 30));
	    logoutPanel.add(getLogoutButton());
	}
	return logoutPanel;
    }

    /**
     * @return
     */
    private PButton getLogoutButton() {
	if (logoutButton == null) {
	    logoutButton = new PButton("Logout",ButtonType.LONG);
	    logoutButton.addActionListener(this);
	}
	return logoutButton;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JPanel getPatientPanel() {
	if (patientPanel == null) {
	    patientPanel = new JPanel();
	    patientPanel.setPreferredSize(new Dimension(200, 450));
	    patientPanel.setLayout(new BorderLayout());
	    patientPanel.setBorder(new LineBorder(ProjectConstants.BORDER_SUB_COLOR,2));
	    patientPanel.add(getPatientTitlePanel(), BorderLayout.NORTH);
	    patientPanel.add(getPatientScrollPane(), BorderLayout.CENTER);
	    patientPanel.add(getPatientButtonPanel(), BorderLayout.SOUTH);
	    patientPanel.addMouseListener(this);
	}
	return patientPanel;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JPanel getPatientTitlePanel() {
	patientTitlePanel = new JPanel() {
	    private static final long serialVersionUID = 1L;

	    /**
	     * custom paintCompontent for friendTitlePanel.
	     * 
	     * @param g
	     *            system Graphics
	     */
	    public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.setColor(new Color(100, 150, 220));
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(Color.white);
		g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 12));
		g.drawString("PATIENTS:", 10, getHeight() - 8);
	    }
	};
	patientTitlePanel.setPreferredSize(new Dimension(200, 25));
	patientTitlePanel.addMouseListener(this);

	return patientTitlePanel;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JPanel getPatientButtonPanel() {
	if (patientButtonPanel == null) {
	    patientButtonPanel = new JPanel();
	    patientButtonPanel.setBorder(new EmptyBorder(10,10,10,10));
	    patientButtonPanel.setLayout(new GridBagLayout());
	    GridBagConstraints gbc1 = new GridBagConstraints();
	    gbc1.gridx = 0;
	    patientButtonPanel.add(getCreatePatient(), gbc1);

	    GridBagConstraints gbc2 = new GridBagConstraints();
	    gbc2.gridx = 1;
	    patientButtonPanel.add(Box.createHorizontalStrut(2), gbc2);

	    GridBagConstraints gbc3 = new GridBagConstraints();
	    gbc3.gridx = 2;
	    patientButtonPanel.add(getDeletePatient(), gbc3);
	    patientButtonPanel.addMouseListener(this);
	}
	return patientButtonPanel;
    }

    /**
     * @author mramsey3
     * @return
     */
    private PButton getCreatePatient() {
	if (createPatient == null) {
	    createPatient = new PButton("Create",ButtonType.SHORT);
	    createPatient.addActionListener(this);
	}
	return createPatient;
    }

    /**
     * @author mramsey3
     * @return
     */
    private PButton getDeletePatient() {
	if (deletePatient == null) {
	    deletePatient = new PButton("Delete",ButtonType.SHORT);
	    deletePatient.addActionListener(this);
	}
	return deletePatient;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JScrollPane getPatientScrollPane() {
	if (patientScrollPane == null) {
	    patientScrollPane = new JScrollPane();
	    patientScrollPane.setPreferredSize(new Dimension(200, 0));
	    patientScrollPane.setViewportView(getPatientList());
	}
	return patientScrollPane;
    }

    /**
     * @author mramsey3
     * @return
     */
    private JList getPatientList() {
	if (patientList == null) {
	    patientList = new JList(new PatientListModel());
	    patientList.setCellRenderer(new PatientListRenderer());
	    patientList.addMouseListener(new DoubleClickListener(patientList));
	}
	return patientList;
    }

    /**
     * @author mramsey3
     * @param p
     */
    public void refreshPanel() {
	patientList.updateUI();
	repaint();
    }

    /**
     * @param pInfo
     */
    private void loadUserDialog(PersonalInfo pInfo) {
	UserProfile userProfile = new UserProfile(pInfo);
	userProfile.setVisible(true);
    }

    /**
     * @author mramsey3
     * 
     */
    private class PatientListModel extends AbstractListModel {
	private static final long serialVersionUID = 1L;

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.ListModel#getElementAt(int)
	 */
	public Object getElementAt(int i) {

	    ArrayList<PersonalInfo> patients = new ArrayList<PersonalInfo>();
	    try {
		patients = MainClient.getConnection().getActivePatients();
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    return patients.get(i);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.ListModel#getSize()
	 */
	public int getSize() {
	    ArrayList<PersonalInfo> patients = new ArrayList<PersonalInfo>();
	    try {
		patients = MainClient.getConnection().getActivePatients();
	    } catch (UserException e) {
		e.printStackTrace();
	    }
	    return patients.size();
	}
    }

    /**
     * @author mramsey3
     * 
     */
    private class DoubleClickListener extends MouseAdapter {
	protected JList list;

	/**
	 * @param l
	 */
	public DoubleClickListener(JList l) {
	    list = l;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
	 */
	public void mouseClicked(MouseEvent e) {
	    if (e.getSource() == patientList)
		if (e.getClickCount() == 2) {
		    if (patientList.getModel().getSize() > 0) {
			int index = list.locationToIndex(e.getPoint());
			ListModel lm = list.getModel();
			PersonalInfo p = (PersonalInfo) (lm.getElementAt(index));
			refreshPanel();
			loadUserDialog(p);
		    }
		}
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent e) {
	if (e.getSource() == getCreatePatient()) {
	    AddPatientDialog apd = new AddPatientDialog(navigationMenuPanel);
	    apd.setVisible(true);
	}
	if (e.getSource() == getDeletePatient()) {
	    switch (JOptionPane.showConfirmDialog(this,
		    "Are you sure you want to delete user?")) {
	    case JOptionPane.YES_OPTION:
		try {
		    int index = patientList.getSelectedIndex();
		    PersonalInfo toBeRemoved = (PersonalInfo) patientList
			    .getSelectedValue();
		    MainClient.getConnection().userSearch(
			    toBeRemoved.getUsername());
		    MainClient.getConnection().deleteSearchUser();
		    ArrayList<PersonalInfo> patients = new ArrayList<PersonalInfo>();
		    try {
			patients = MainClient.getConnection()
				.getActivePatients();
		    } catch (UserException e3) {
			e3.printStackTrace();
		    }
		    if (patients.size() == index) {
			patientList.setSelectedIndex(patients.size() - 1);
			System.out.println("hey--I reset selection!");
		    }
		    refreshPanel();
		} catch (UserException e1) {
		    e1.printStackTrace();
		}
		break;
	    case JOptionPane.NO_OPTION:
		break;
	    case JOptionPane.CANCEL_OPTION:
		break;
	    default:

	    }
	} else if (e.getSource() == viewWaitingAppointments) {
	    WaitingAppointmentsDialog wad = new WaitingAppointmentsDialog();
	    wad.setVisible(true);
	} else if (e.getSource() == logoutButton) {
	    MainClient.logOut();
	}
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }
}
